package com.ybkj.daijia.config;

import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

@Component
public class PrivateApi {

    private static final Logger logger = LoggerFactory.getLogger(PrivateApi.class);
    /**
     * 分页查询新闻列表.
     */
    public static String QUERY_NEWS = "api/noticenews/selectCountNoticeNews";
    /**
     * 保存新闻.
     */
    public static String SAVE_NEWS = "api/noticenews/insertNoticeNews";
    /**
     * 修改新闻.
     */
    public static String UPDATE_NEWS = "api/noticenews/updateNoticeNews";
    /**
     * 删除新闻.
     */
    public static String DELETE_NEWS = "api/noticenews/deleteNoticeNews";
    /**
     * 查询单个新闻;
     */
    public static String QUERY_ONE_NEWS = "api/noticenews/selectNoticeNews";
    @Value("${module.api.url}")
    private String privateapiBaseURL;
    @Autowired
    private SettingUtils settingUtils;
    @Autowired
    private RestTemplate restTemplate;

    public String getPrivateapiBaseURL() {
        return privateapiBaseURL.endsWith("/") ? privateapiBaseURL : privateapiBaseURL + "/";
    }

    public void setPrivateapiBaseURL(String privateapiBaseURL) {
        this.privateapiBaseURL = privateapiBaseURL;
    }

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String get(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appkey", setting.getSecKey());
        String result = null;
        try {
            result = restTemplate.getForObject(apiurl, String.class, kv);
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            result = e.getResponseBodyAsString();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            result = e.getResponseBodyAsString();
        }
        return result;

    }

    /**
     * 调用api获取新闻列表
     *
     * @param apiurl  地址
     * @param kv      参数列表
     * @param setting 系统设置
     * @return
     */
    public String getNewsByApp(String apiurl, Map<String, String> kv, Setting setting) {
        String result = null;
        try {
            result = restTemplate.getForObject(apiurl, String.class, kv);
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            result = e.getResponseBodyAsString();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            result = e.getResponseBodyAsString();
        }
        return result;

    }

    public String postMultiValueMap(String url, MultiValueMap<String, String> kv, Setting setting) {

        kv.add("appkey", setting.getSecKey());

        Set<Entry<String, List<String>>> entries = kv.entrySet();

        Map<String, String> params = new HashMap<String, String>();
        for (Entry<String, List<String>> entry : entries) {
            List<String> values = entry.getValue();
            params.put(entry.getKey(), CollectionUtils.isEmpty(values) ? null : values.get(0));
        }

        RestTemplate rest = new RestTemplate();

        String result = null;

        try {
            result = rest.postForObject(url, kv, String.class);
        } catch (HttpClientErrorException e) {
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }

        return result;
    }

    public String post(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appkey", setting.getSecKey());
        String result = null;
        try {
            result = restTemplate.postForObject(apiurl, null, String.class, kv);
        } catch (HttpClientErrorException e) {
            logger.error("HttpClientErrorException:code:{},status:{},body:{}", e.getStatusCode(),
                e.getStatusText(), e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }
        return result;
    }

    public String put(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appkey", setting.getSecKey());
        String result = null;
        try {
            restTemplate.put(apiurl, null, kv);
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            result = e.getResponseBodyAsString();
        }
        return result;
    }

    public String delete(String apiurl, Map<String, String> kv, Setting setting) {
        kv.put("appkey", setting.getSecKey());
        String result = null;
        try {
            restTemplate.delete(apiurl, kv);
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        } catch (HttpServerErrorException e) {
            logger.error("HttpServerErrorException:{}", e.getResponseBodyAsString());
            result = e.getResponseBodyAsString();
        }
        return result;
    }

}
